var ul1 = document.querySelector('.sptj .box ul')
var fenye = document.querySelector('.sptj .box .fenye')
var search1 = getCookie('search1')
var current = 1
requestDate()

function requestDate() {
    sendAjax({
        url: 'http://localhost:8888/goods/list',
        data: {
            current,
            search: search1,
            pagesize: 20
        },
        success: res => {
            console.log(res)
            var {
                code,
                list,
                total
            } = res
            if (code === 1) {
                document.querySelector('.search_22').value = search1
                var str = ''
                list.forEach(v => {
                    str += `
                    <li>
                            <a href="detail.html?id=${v.goods_id}">
                                <img src="${v.img_big_logo}" alt="">
                                <span class="title">${v.title}</span>
                                <br>
                                <span class="price">￥<em>${v.price}</em></span>
                                
                            </a>
                        </li>
                    `
                });

                ul1.innerHTML = str
                var pageStr = `
                    <div class="first" onclick="first()">首页</div>
                    <div class="prev" onclick="prev()">上一页</div>
                    <div class="list">
                `
                if (total <= 5) {
                    pageStr += createP(1, total)
                } else {

                    if (current <= 3) {

                        pageStr += createP(1, 5)
                    } else if (current >= total - 2) {

                        pageStr += createP(total - 4, total)
                    } else {

                        pageStr += createP(current - 2, current + 2)

                    }
                }
                pageStr += `
                </div>
                <div class="next" onclick="next(${total})">下一页</div>
                <div class="last" onclick="last(${total})">尾页</div>
                `
                fenye.innerHTML = pageStr


            }
        }
    })
}

function prev() {

    current--

    if (current <= 1) {
        current = 1
        return
    }

    requestDate()
}



function first() {

    if (current === 1) return

    current = 1

    requestDate()
}


function last(total) {

    if (current === total) return

    current = total

    requestDate()
}

function next(total) {

    current++

    if (current >= total) {
        current = total
        return
    }

    requestDate()
}


function toCurrent(i) {


    if (current === i) return

    current = i

    requestDate()
}


function createP(start, end) {

    var str = ''

    for (i = start; i <= end; i++) {

        if (i === current) {
            str +=
                `
                <p onclick="toCurrent(${i})" class="active">${i}</p>
                `
        } else {
            str +=
                `
                <p onclick="toCurrent(${i})">${i}</p>
                 `
        }
    }
    return str
}